package com.power.using.askapp.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.power.using.askapp.R;
import com.power.using.askapp.base.BaseMainFragment;
import com.power.using.askapp.bean.HomeBean;
import com.power.using.askapp.eventbus.StartNewFragmentEvent;
import com.power.using.askapp.http.HttpApi;
import com.power.using.askapp.http.Request;
import com.power.using.askapp.interfaces.Constant;
import com.power.using.askapp.utils.HttpUtils;
import com.power.using.askapp.utils.RegexUtils;
import com.power.using.askapp.utils.SPUtil;
import com.power.using.askapp.view.SelectDialogView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.power.using.askapp.utils.loading.LoadingDialog.cancelLoadingDialog;

/**
 * Created by 聪明一只哈 on 2018/5/25.
 * Time: 2018/5/25  10:35
 */

public class HomeFragment extends BaseMainFragment implements View.OnClickListener {


    Unbinder unbinder;
    @BindView(R.id.toolbar_title)
    TextView toolTitle;
    @BindView(R.id.et_question_content)
    EditText etQuestionContent;
    @BindView(R.id.et_question_beizhu)
    EditText etQuestionBeizhu;
    @BindView(R.id.bt_question_commit)
    Button btQuestionCommit;
    @BindView(R.id.bt_judge)
    Button btJudge;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.tv_tel)
    EditText tvTel;
    private SelectDialogView selectDialogView;

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }


    private void initView(View view) {
        toolTitle.setText("提交问题");
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initCheckData();
        initListener();
        Log.i("checkfragment", "onLazyInitView: ");
    }

    @Override
    public void onStart() {
        super.onStart();

        Log.i("checkfragment", "onStart: ");


    }

    /**
     * 先对之前的问题进行检查
     * 检查有木有进行评价
     */
    private void initCheckData() {
        /**
         *
         * 把initCheckData()的功能添加到对应的点击按钮上
         *
         * 这里访问服务器查询是否有未评价的问题,尚若有则弹出dialog,提示去评价,确定按钮跳转到评价页面
         *
         * 没有未评价过的问题,则正常输入
         * */

        String isclear = SPUtil.getString(Constant.ISCLEAR, "");
        if(!TextUtils.isEmpty(isclear)&&"0".equals(isclear)){
            selectDialogView = new SelectDialogView(getActivity());
            selectDialogView.builderEidt()
                    .setViewGone()
                    .setOutClick(false)
                    .setTitle("提示")
                    .setMsg("您有未评价的问题,请先进行评价")
                    .setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            EventBus.getDefault().post(new StartNewFragmentEvent(JudgeFragment.newInstance()));
                            SelectDialogView.cancle();
                        }
                    }
            ).show();
        }




    }

    private void initListener() {
        btQuestionCommit.setOnClickListener(this);

        btJudge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new StartNewFragmentEvent(JudgeFragment.newInstance()));

            }
        });
    }

    @Override
    public void onClick(View v) {

        String isclear = SPUtil.getString(Constant.ISCLEAR, "");
        if(!TextUtils.isEmpty(isclear)&&"0".equals(isclear)){
            selectDialogView = new SelectDialogView(getActivity());
            selectDialogView.builderEidt()
                    .setViewGone()
                    .setOutClick(false)
                    .setTitle("提示")
                    .setMsg("您有未评价的问题,请先进行评价")
                    .setPositiveButton("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    EventBus.getDefault().post(new StartNewFragmentEvent(JudgeFragment.newInstance()));
                                    SelectDialogView.cancle();
                                }
                            }
                    ).show();
            return;
        }

        final String tvadd = tvAdd.getText().toString().trim();
        final String tvtel = tvTel.getText().toString().trim();
        String questionCoPntent = etQuestionContent.getText().toString().trim();

        if(!RegexUtils.isMobileExact(tvtel)){
            Toast.makeText(getActivity(), "电话号码格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(questionCoPntent)) {
            Toast.makeText(getActivity(), "请先输入问题内容", Toast.LENGTH_SHORT).show();
            return;
        }


        Log.i("hashiqi_question", "CommitQuestion: " + questionCoPntent);



        Log.i("hashiqi_question", "CommitQuestion: " + questionCoPntent + "~~" + tvadd + "~~" + tvtel);

        //etQuestionContent.setText("" + tvadd + "~~" + tvtel);
        Map<String,String> params=new HashMap<>();
        params.put("address",tvadd);
        params.put("telephone",tvtel);

        HttpUtils.requestPost(_mActivity, HttpApi.PUT_INFO,params, new Request(){
            @Override
            public void success(String result) {
                super.success(result);
                Log.i("show", "success: "+result);
                HomeBean homeBean = new Gson().fromJson(result, HomeBean.class);
                if(homeBean.getCode()==200){
                    Toast.makeText(getActivity(), "提交问题成功", Toast.LENGTH_SHORT).show();
                    tvTel.setText("");
                    etQuestionContent.setText("");
                    etQuestionBeizhu.setText("");
                    SPUtil.putString(Constant.ISCLEAR,"0");
                }
            }

            @Override
            public void error(String error) {
                super.error(error);
                Log.i("show", "error: "+error);
            }
        });


    }



    @Override
    public void onStop() {
        super.onStop();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();
    }


}
